package com.group.service;

import com.group.dao.ClokinDao;
import com.group.dao.GeneralBeanDao;
import com.group.model.ClockinChallenge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ClokinService {

    @Autowired
    ClokinDao clokinDao;

    @Autowired
    GeneralBeanDao beanDao;

    @Autowired
    TickerService tickerService;

    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

    /**
     * 获取日期
     *
     * @return
     */
    public String getDayDate(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, day);
        return format.format(calendar.getTime());
    }

    /**
     * 判断时间范围
     *
     * @return
     */
    public boolean judgeTimeScope(String timeStatus) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
        LocalDateTime localTime = LocalDateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), dtf);

        LocalDateTime startTime = LocalDate.now().atTime(6, 0, 0);
        LocalDateTime endTime = LocalDate.now().atTime(8, 0, 0);

        if (timeStatus.equals("战况") && localTime.isAfter(endTime)) {
            //时间08：00之后,显示今日新一轮的信息
            return true;

        } else if (timeStatus.equals("打卡") && localTime.isAfter(startTime) && localTime.isBefore(endTime)) {
            //打卡时间  06:00-08:00
            return true;

        } else if (timeStatus.equals("睡眠")) {
            //睡眠时间  0:00-06：00
            startTime = LocalDate.now().atTime(0, 0, 0);
            endTime = LocalDate.now().atTime(6, 0, 0);
            if (localTime.isAfter(startTime) && localTime.isBefore(endTime)) {
                return true;
            }
        }

        return false;
    }


    /**
     * 每天8点之后，对早起成功者，进行结算奖励金
     */
    @Scheduled(cron = "0 0 8 * * ?")
    @Transactional
    public void giveReward() {

        String yesterday = getDayDate(-1);

        //状态改成失败
        clokinDao.updateFailedStatus(yesterday);

        //连续签到次数：打卡失败者置0
        clokinDao.updateFailedClikinCount(yesterday);

        List<ClockinChallenge> list = clokinDao.queryDayPersons(yesterday, "早起成功");

        if (list.isEmpty()) {
            return;
        }

        Double successWeccSum = clokinDao.queryPutWeccSum(yesterday, "早起成功");
        Double losersWeccSum = clokinDao.queryPutWeccSum(yesterday, "早起失败");

        //Double total = successWeccSum + losersWeccSum;

        for (ClockinChallenge clockins : list) {

            Double incomeWecc = clockins.getPutWecc() * (losersWeccSum / successWeccSum) + clockins.getPutWecc();
            //Double incomeWecc = losersWeccSum * (clockins.getPutWecc() / total) + clockins.getPutWecc();

            tickerService.addTicker(clockins.getUserId(), "wecc", incomeWecc, "早起打卡奖励金转入");
            clockins.setIncomeWecc(incomeWecc);
            beanDao.save(clockins);
        }

        //连续签到次数：打卡成功者+1
        clokinDao.updateSuccessClikinCount(yesterday);
    }

}
